<?php


namespace App\Http\Middleware;

use App\Tools\Verify\ApplyAPIVerify;
use Closure;

class ClientMiddleware

{
    private static $verify; // 验签实例
    private static $defaultGuid = 'jdguid2023'; // 默认guid
    private static $defaultToken = 'token202311'; // 默认token
    private static $hashs = [
        [2, 18, 19, 23, 30, 31],
        [10, 4, 1, 17, 22, 29],
        [6, 16, 31, 0, 12, 18],
        [4, 12, 21, 1, 5, 10],
        [8, 20, 12, 28, 25, 20],
        [12, 28, 13, 19, 20, 21],
        [10, 24, 17, 27, 11, 22],
        [14, 10, 20, 18, 16, 12]
    ]; // 哈希表
    private static $strArr = [2, 8, 7]; // 截取数组
    private static $order = [0, 1, 2, 3]; // 加密顺序
    private static $commonPath = [
        'web/user/register',
        'web/user/login',
        'web/user/find-pass',
        'web/common/content/all',
        'web/common/content/info',
        'web/common/tag/all',
        'web/product/map/panorama/info',
        'web/product/map/all/panorama'
    ]; // 通用接口

    private function verify($request)
    {
        if (env('NO_VERIFY_SIGN')) return 20000;

        $path = $request->path();

        self::$verify = new ApplyAPIVerify([
            'time' => $request->header('App-Time'),
            'userGuid' => $request->header('App-User'),
            'signatures' => $request->header('App-Signatures'),
            'path' => '/' . $path,
            'defaultGuid' => self::$defaultGuid,
            'defaultToken' => self::$defaultToken,
            'hashs' => self::$hashs,
            'strArr' => self::$strArr,
            'order' => self::$order,
            'redisPrefix' => config('rediskey')['userClientInfo'],
        ]);

        if (in_array($path, self::$commonPath)) {

            return self::$verify->common($request);
        } else {
            return self::$verify->proprietary($request);
        }
    }

    public function handle($request, Closure $next)
    {
        $time = time();
        $key = $this->verify($request);

        switch ($key) {
            case '20000':
                return $next($request);
            case "40001":
                return response()->json(['serverTime' => $time, 'code' => '40001', 'data' => 'Server internal error!']);
                break;
            case "40002":
                return response()->json(['serverTime' => $time, 'code' => '40002', 'data' => 'Request timeout!']);
                break;
            case "40003":
                return response()->json(['serverTime' => $time, 'code' => '40003', 'data' => 'Version number exception!']);
                break;
            case "40004":
                return response()->json(['serverTime' => $time, 'code' => '40004', 'data' => 'Global user ID can not be null!']);
                break;
            case "40005":
                return response()->json(['serverTime' => $time, 'code' => '40005', 'data' => 'Signature error!']);
                break;
            case "40007":
                return response()->json(['serverTime' => $time, 'code' => '40007', 'data' => 'user not!']);
                break;
            case "40008":
                return response()->json(['serverTime' => $time, 'code' => '40008', 'data' => 'Other devices login!']);
                break;
            case "40009":
                return response()->json(['serverTime' => $time, 'code' => '40009', 'data' => 'token time out!']);
                break;
            case "40010":
                return response()->json(['serverTime' => $time, 'code' => 'SN010', 'data' => 'Permission denied']);
                break;
            default:
                return response()->json(['serverTime' => $time, 'code' => '40006', 'data' => 'No access!']);
        }
    }
}
